package edu.unc.ils.mrc.vocabulary;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import maui.stemmers.Stemmer;

import edu.unc.ils.pos.POSFilter;

public abstract class Vocabulary 
{
    
    Stemmer stemmer = null;
    POSFilter filter = null;
    List<String> stopwords = new ArrayList<String>();

    public abstract int getSize() throws Exception;
    
    public abstract String getPrefLabel(int conceptId) throws Exception;
    
    public abstract int getConceptId(String prefLabel) throws Exception;
    
    public abstract void open(String path) throws Exception;
    
    public abstract void close() throws Exception;
    
    public abstract int getNextRandom(int conceptId) throws Exception;
    
    public abstract int getNextRandom(int conceptId, int btWeight, int ntWeight, int rtWeight)
        throws Exception;
    
    public abstract boolean isRelated(String term1, String term2) throws Exception;
    
    public abstract boolean isBroaderNarrower(String term1, String term2) throws Exception;
    
    public void setPOSFilter(POSFilter filter) 
    {
        this.filter = filter;
    }
    
    public void setStemmer(Stemmer stemmer) 
    {
        this.stemmer = stemmer;
    }
    
    protected String pseudoPhrase(String str)
    {
        String pseudoPhrase = "";
        
        String []terms = str.split(" ");
        for (String term: terms)
        {
            StringBuffer buf = new StringBuffer();
            char[] cs = term.toCharArray();
            for (char c: cs) {
                if (Character.isLetter(c))
                    buf.append(c);
                else
                    buf.append(" ");
            }
            if (!isStopword(buf.toString()))
                pseudoPhrase += stem(buf.toString()) + " ";
        }
        return pseudoPhrase.toLowerCase().trim();
    }
    
    /**
     * Joins an array of strings to a single string.
     */
    protected static String join(String[] str) {
        String result = "";
        for(int i = 0; i < str.length; i++) {
            if (result != "") {
                result = result + " " + str[i];
            } else {
                result = str[i];
            }
        }
        return result;
    }

    
    protected String stem(String str)
    {
        if (stemmer != null)
            return stemmer.stem(str);
        else
            return str;
    }
    
    public void setStopwords(List<String> stopwords) 
    {
        this.stopwords = stopwords;
    }
    
    public void setStopwords(String path) throws IOException 
    {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String word;
        while ((word = br.readLine()) != null)
            stopwords.add(word);
    }
    
    public boolean isStopword(String str) {
        return stopwords.contains(str);
    }
    
    public abstract List<Term> lookup(String str) throws Exception;
    public abstract List<Term> lookupNonStemmed(String str) throws Exception;
    public abstract List<Term> lookupWord(String str) throws Exception;
    public abstract List<Term> lookupWordStemmed(String str) throws Exception;
    
}
